/**
 * 
 */
package cn.agiledata.bank.account.service.impl;

import java.util.Date;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.agiledata.bank.account.dao.ISequenceDAO;
import cn.agiledata.bank.account.service.ISequenceGenerator;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;

/**
 * @author 马贤波
 * 
 * Apr 6, 2006
 */
public class DefaultSequenceGeneratorImpl implements ISequenceGenerator {

	private static Log log = LogFactory
			.getLog(DefaultSequenceGeneratorImpl.class);

	private ISequenceDAO seqDAO;

	public String getTransactionSequence(Date date, String sequenceNo) {
		String yyyyMMdd = DateUtil.date2String(date, "yyyyMMdd");
		while (sequenceNo.length() < 7) {
			sequenceNo = "0" + sequenceNo;
		}
		return yyyyMMdd + sequenceNo;
	}

	public synchronized String getTransactionSequence() {
		String seqStr;
		try {
			seqStr = this.seqDAO.retrieveSeqByType(Constant.ID_TX);
		} catch (Exception e) {
			log.error(e);
			seqStr = String.valueOf(this.getRandomNum());
		}
		if (seqStr.length() > 6) {
			seqStr = seqStr.substring(seqStr.length() - 6);
		}

		String result = getTransactionSequence(new Date(), seqStr);
		log.info("取得交易流水号：" + result);
		return result;
	}
	
	public synchronized String getBarCodeOrderId() {
		String yyyyMMdd = DateUtil.date2String(new Date(), "yyyyMMdd");
		String seqStr;
		try {
			seqStr = this.seqDAO.retrieveSeqByType(Constant.ID_BARCODE);
		} catch (Exception e) {
			log.error(e);
			seqStr = String.valueOf(this.getRandomNum());
		}
		if (seqStr.length() > 6) {
			seqStr = seqStr.substring(seqStr.length() - 6);
		}

		String result = "B"+yyyyMMdd+seqStr;
		log.info("取得交易流水号：" + result);
		return result;
	}

	public synchronized String getUpauditApplyId(){
		String yyyyMMdd = DateUtil.date2String(new Date(), "yyyyMMdd");
		String seqStr;
		try {
			seqStr = this.seqDAO.retrieveSeqByType(Constant.ID_UPADJUST);
		} catch (Exception e) {
			log.error(e);
			seqStr = String.valueOf(this.getRandomNum());
		}
		if (seqStr.length() > 3) {
			seqStr = seqStr.substring(seqStr.length() - 3);
		}

		String result = yyyyMMdd+seqStr;
		log.info("取得调账号：" + result);
		return result;
	}
	
	public synchronized String getTransactionSequence(String userType,
			String txType) {

		String prefix = Constant.ID_PREFIX;

		String seqStr;
		try {
			seqStr = this.seqDAO.retrieveSeqByType(Constant.ID_TX);
		} catch (Exception e) {
			log.error(e);
			seqStr = String.valueOf(this.getRandomNum());
		}

		StringBuffer result = new StringBuffer();
		result.append(prefix);
		result.append(userType);
		result.append(txType);
		result.append(seqStr);

		log.info("取得交易流水号：" + result.toString());
		return result.toString();
	}

	public String getAccountSequence(String registerPerson,
			String registerMethod) {
		String seqStr;
		try {
			seqStr = this.seqDAO.retrieveSeqByType(Constant.ID_ACCOUNT);
		} catch (Exception e) {
			return null;
		}
		StringBuffer result = new StringBuffer();
		result.append(registerPerson);
		result.append(registerMethod);
		result.append(seqStr);
		return result.toString();
	}

	private int getRandomNum() {
		Random rand = new Random();
		return rand.nextInt(1000000);
	}

	public void afterPropertiesSet() throws Exception {

	}

	/**
     * @return Returns the seqDAO.
     */
	public ISequenceDAO getSeqDAO() {
		return seqDAO;
	}

	/**
     * @param seqDAO
     *            The seqDAO to set.
     */
	public void setSeqDAO(ISequenceDAO seqDAO) {
		this.seqDAO = seqDAO;
	}
	/**
	 * 得到付款回单的sequence
	 * @return
	 */
	public synchronized String getPayPaperSequence() {
		String seqStr;
		seqStr = this.seqDAO.retrieveSeqByType(Constant.ID_PAY_PAPER);
		String result = getPaperSequence(new Date(), seqStr);
		return result;
	}
	/**
	 * 得到收款回单的sequence
	 * @return
	 */
	public synchronized String getReceivePaperSequence() {
		String seqStr;
		seqStr = this.seqDAO.retrieveSeqByType(Constant.ID_RECEIVE_PAPER);
		String result = getPaperSequence(new Date(), seqStr);
		return result;
	}
	/**
	 * 得到内部转帐回单的sequence
	 * @return
	 */
	public synchronized String getTransferPaperSequence() {
		String seqStr;
		seqStr = this.seqDAO.retrieveSeqByType(Constant.ID_TRANSFER_PAPER);
		String result = getPaperSequence(new Date(), seqStr);
		return result;
	}
	/**
	 * 
	 * @param date
	 * @param sequenceNo
	 * @return
	 */
	public String getPaperSequence(Date date, String sequenceNo) {
		String yyyy = DateUtil.date2String(date, "yyyy");
		while (sequenceNo.length() < 6) {
			sequenceNo = "0" + sequenceNo;
		}
		return yyyy + sequenceNo;
	}
    
    public synchronized String getTransactionBatchNo(){
        String seqStr;
        try {
            seqStr = this.seqDAO.retrieveSeqByType(Constant.ID_BATCH_PAY);
        } catch (Exception e) {
            log.error(e);
            seqStr = String.valueOf(this.getRandomNum());
        }
        if (seqStr.length() > 4) {
            seqStr = seqStr.substring(seqStr.length() - 4);
        }
        String yyyyMMdd = DateUtil.date2String(new Date(), "yyyyMMdd");
        return yyyyMMdd + seqStr;
    }
    
    public synchronized String getMobileChargeRecordNo() {
		String yyyyMMdd = DateUtil.date2String(new Date(), "yyyyMMdd");
		String seqStr;
		try {
			seqStr = this.seqDAO.retrieveSeqByType(Constant.ID_MOBILE_CHARGE);
		} catch (Exception e) {
			log.error(e);
			seqStr = String.valueOf(this.getRandomNum());
		}
		if (seqStr.length() > 6) {
			seqStr = seqStr.substring(seqStr.length() - 6);
		}

		String result = "M"+yyyyMMdd+seqStr;
		log.info("Generate Mobile Charge Record Order No：" + result);
		return result;
	}

}
